
/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) Nginx, Inc.
 */

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_log.h>
#include <ngx_atomic.h>
static ngx_atomic_t   connection_counter = 1;
ngx_atomic_t  *ngx_mars_stat_requests=&connection_counter;
static ngx_int_t ngx_http_mars_status_hander(ngx_http_request_t *r);
static char * ngx_http_mars_status(ngx_conf_t *cf,ngx_command_t *cmd,
    void *conf);
static ngx_command_t ngx_http_mars_status_command[] = {
    {ngx_string("mars_status"),//定义模块名称
    NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE1,//定义配置出现的位置
    ngx_http_mars_status,
    0,
    0,
    NULL
    },
    ngx_null_command
};

static ngx_http_module_t  ngx_http_mars_status_module_ctx = {
    NULL,    /* preconfiguration */
    NULL,                                  /* postconfiguration */

    NULL,                                  /* create main configuration */
    NULL,                                  /* init main configuration */

    NULL,                                  /* create server configuration */
    NULL,                                  /* merge server configuration */

    NULL,                                  /* create location configuration */
    NULL                                   /* merge location configuration */
};
ngx_module_t ngx_http_mars_nginx_status_module = {
    NGX_MODULE_V1,//定义model版本
    &ngx_http_mars_status_module_ctx,
    ngx_http_mars_status_command,//加入command参数
    NGX_HTTP_MODULE,
    NULL,                                  /* init master */
    NULL,                                  /* init module */
    NULL,                                  /* init process */
    NULL,                                  /* init thread */
    NULL,                                  /* exit thread */
    NULL,                                  /* exit process */
    NULL,                                  /* exit master */
    NGX_MODULE_V1_PADDING
};

static char* ngx_http_mars_status(ngx_conf_t *cf,ngx_command_t *cmd,
    void *conf){
    ngx_http_core_loc_conf_t *clcf;
    //拿到location的配置文件
    clcf  = ngx_http_conf_get_module_loc_conf(cf,ngx_http_core_module);
    clcf->handler=ngx_http_mars_status_hander;
    return NGX_CONF_OK;
}

static ngx_int_t ngx_http_mars_status_hander(ngx_http_request_t *r){
    if(!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))){
        return NGX_HTTP_NOT_ALLOWED;
    }
    ngx_atomic_int_t num = ngx_atomic_fetch_add(ngx_mars_stat_requests, 1);
    ngx_int_t rc = ngx_http_discard_request_body(r);
    if(rc != NGX_OK){
        return rc;
    }
    ngx_str_t type = ngx_string("text/plain");
    ngx_str_t response = ngx_string("hello_world");
    
    // r->headers_out.content_type.len = sizeof("text/plain") - 1;
    // r->headers_out.content_type.data = (u_char*)"text/plain";


    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_length_n = response.len;
    r->headers_out.content_type = type;
    
    //发送http头部
    rc = ngx_http_send_header(r);
    if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
        return rc;
    }
    ngx_buf_t *buf;
    buf = ngx_create_temp_buf(r->pool,response.len);
    if(buf == NULL){
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }
    ngx_memcpy(buf->pos,response.data,response.len);
    buf->last = buf->pos+response.len;
    //申明这是最后一块缓冲区
    buf->last_buf = 1;
    ngx_chain_t out;
    out.buf = buf;
    out.next = NULL;
    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                           "#####%d",num,NULL);
    return ngx_http_output_filter(r, &out);
}